package com.qeegoo.b2bautozityp.moudle.good.view.carmodel;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.databinding.FragGoodsCarmodelTwoBinding;
import com.qeegoo.b2bautozityp.injector.component.DaggerGoodsFragmentComponent;
import com.qeegoo.b2bautozityp.moudle.base.BaseFragment;
import com.qeegoo.b2bautozityp.moudle.good.model.CarModelBean;
import com.qeegoo.b2bautozityp.moudle.good.view.GoodsActivity;
import com.qeegoo.b2bautozityp.moudle.good.viewmodel.carmodel.CarModelBeanViewModel;
import com.qeegoo.b2bautozityp.moudle.good.viewmodel.carmodel.CarModelTwoViewModel;
import com.qeegoo.b2bautozityp.moudle.good.viewmodel.carmodel.CarModelYearViewModel;

import java.util.ArrayList;

import javax.inject.Inject;

import base.lib.util.ActivityManager;
import base.lib.util.ToastUtils;
import base.lib.widget.recyclerview.DividerLinearItemDecoration;
import rx.Observable;

/**
 * 王少岩 在 2017/9/25 创建了它
 */

public class CarModelTwoFragment extends BaseFragment<FragGoodsCarmodelTwoBinding> implements View.OnClickListener {
    @Inject
    CarModelTwoViewModel mViewModel;

    private String title, seriesIds;
    private String flag = "year", flag_all = "";
    private TextView tvSelected;
    private ArrayList<CarModelBean> sel_list = new ArrayList<>();

    public static CarModelTwoFragment newInstance(String title, String seriesIds) {
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putString("seriesIds", seriesIds);
        CarModelTwoFragment fragment = new CarModelTwoFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.frag_goods_carmodel_two;
    }

    @Override
    protected void initInjector() {
        DaggerGoodsFragmentComponent.builder().appComponent(getAppComponent()).fragmentComponent(getFragmentComponent()).build().inject(this);
    }

    @Override
    protected void initViews() {
        title = getArguments().getString("title");
        seriesIds = getArguments().getString("seriesIds");
        mViewModel.title.set(title);
        mBinding.setViewModel(mViewModel);
        initRv();
        getData();
        setListener();
        tvSelected.setText("已选择" + ((GoodsActivity) getActivity()).getViewModel().getSelectBeans().size() + "款车型");
    }

    private void initRv() {
        mViewModel.getAdapterBean().addHeaderView(getHeaderView_all());
        mViewModel.getAdapterBean().addHeaderView(getHeaderView_selected());
        mBinding.rvGoodsCarmodel.setAdapter(mViewModel.getAdapterBean());
        mBinding.rvGoodsCarmodel.addItemDecoration(new DividerLinearItemDecoration(getContext(),
                DividerLinearItemDecoration.HORIZONTAL_LIST,
                (int) getResources().getDimension(R.dimen.dimen_1),
                getResources().getColor(R.color.line_border)));
        mBinding.rvGoodsCaryear.setAdapter(mViewModel.getAdapterYear());
        mBinding.rvGoodsCaryear.addItemDecoration(new DividerLinearItemDecoration(getContext(),
                DividerLinearItemDecoration.HORIZONTAL_LIST,
                (int) getResources().getDimension(R.dimen.dimen_1),
                getResources().getColor(R.color.line_border)));
        mBinding.rvGoodsCaryear.addItemDecoration(new DividerLinearItemDecoration(getContext(),
                DividerLinearItemDecoration.VERTICAL_LIST,
                (int) getResources().getDimension(R.dimen.dimen_1),
                getResources().getColor(R.color.line_border)));
    }

    private View getHeaderView_all() {
        View headerView = LayoutInflater.from(getActivity()).inflate(R.layout.layout_header, null);
        headerView.setOnClickListener(v -> {
            if (flag_all.equals("")) {
                flag_all = "all";
                sel_list.clear();
                Observable.from(mViewModel.getAdapterBean().getData())
                        .subscribe(viewModel -> {
                            viewModel.isSelected.set(true);
                            sel_list.add(viewModel.mBean);
                        });
            } else {
                flag_all = "";
                Observable.from(mViewModel.getAdapterBean().getData())
                        .subscribe(viewModel -> {
                            viewModel.isSelected.set(false);
                            sel_list.remove(viewModel.mBean);
                        });
            }
        });
        return headerView;
    }

    private View getHeaderView_selected() {
        View headerView = LayoutInflater.from(getActivity()).inflate(R.layout.layout_header_selected, null);
        tvSelected = (TextView) headerView.findViewById(R.id.tv_selected_carmodel);
        return headerView;
    }


    private void getData() {
        mViewModel.getCarYear(seriesIds);
        mViewModel.getCarModel(seriesIds, "", "");
    }

    private void setListener() {
        setOnClickListener(this, mBinding.layoutYear, mBinding.layoutAppbar, mBinding.layoutAllyear, mBinding.layoutAllpl, mBinding.tvConfirm);
        mViewModel.getAdapterBean().setOnItemClickListener((adapter, view, position) -> {
            CarModelBeanViewModel viewModel = (CarModelBeanViewModel) adapter.getItem(position);
            viewModel.isSelected.set(!viewModel.isSelected.get());
            if (viewModel.isSelected.get()) {
                sel_list.add(viewModel.mBean);
            } else {
                sel_list.remove(viewModel.mBean);
            }
        });
        mViewModel.getAdapterYear().setOnItemClickListener((adapter, view, position) -> {
            CarModelYearViewModel viewModel = (CarModelYearViewModel) adapter.getItem(position);
            String str = viewModel != null ? viewModel.name.get() : "";
            if (flag.equals("year")) {
                String capacity = mBinding.tvPl.getText().toString().equals("全部排量") ? "" : mBinding.tvPl.getText().toString();
                mViewModel.getCarModel(seriesIds, str, capacity);
                mBinding.tvYear.setText(str);
            } else if (flag.equals("capacity")) {
                String year = mBinding.tvYear.getText().toString().equals("全部年款") ? "" : mBinding.tvYear.getText().toString();
                mViewModel.getCarModel(seriesIds, year, str);
                mBinding.tvPl.setText(str);
            }
            mBinding.layoutYear.setVisibility(View.GONE);
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_year:
            case R.id.layout_appbar:
                flag = "";
                mBinding.layoutYear.setVisibility(View.GONE);
                break;
            case R.id.layout_allyear:
                if (mBinding.layoutYear.getVisibility() == View.VISIBLE && flag.equals("year")) {
                    mBinding.layoutYear.setVisibility(View.GONE);
                    flag = "";
                } else {
                    flag = "year";
                    mBinding.layoutYear.setVisibility(View.VISIBLE);
                    mViewModel.showYear();
                }
                break;
            case R.id.layout_allpl:
                if (mBinding.layoutYear.getVisibility() == View.VISIBLE && flag.equals("capacity")) {
                    mBinding.layoutYear.setVisibility(View.GONE);
                    flag = "";
                } else {
                    flag = "capacity";
                    mBinding.layoutYear.setVisibility(View.VISIBLE);
                    mViewModel.showCapacity();
                }
                break;
            case R.id.tv_confirm:
                if (sel_list.size() > 0) {
                    ((GoodsActivity) ActivityManager.getActivity()).getViewModel().getSelectBeans().addAll(sel_list);
                    ((GoodsActivity) ActivityManager.getActivity()).setProperty("适用车型", "", "已选择" + ((GoodsActivity) ActivityManager.getActivity()).getViewModel().getSelectBeans().size() + "款车型");
                } else {
                    ToastUtils.showToast("请选择车型");
                }
                break;
        }
    }
}
